<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

</head>
<!-- 第一个helloworld程序 -->

<body>
  <div id="app"></div>
  <template id="root">
    <div>
      <ul>
        <li v-for="item in list"><input type="checkbox" v-select="item.checked">
          {{item.name}}
        </li>
      </ul>
      <button @click="checkAll">全选</button>
      <button @click="uncheckAll">全不选</button>
    </div>
  </template>
</body>
<script>
  const app = Vue.createApp({
    template: "#root",
    data() {
      return {
        list: [{ name: "足球", checked: false }, { name: "篮球", checked: false }, { name: "乒乓球", checked: true }]
      }
    },
    directives: {
      select: {
        mounted(el, binding) {
          el.checked = binding.value
        },
        updated(el, binding) {
          el.checked = binding.value

        }
      }
    },
    methods: {
      checkAll() {
        this.list.forEach(e => {
          e.checked = true
        });
      },
      uncheckAll() {
        this.list.forEach(e => {
          e.checked = false
        });


      }
    }
  })
  app.mount("#app")
</script>

</html>